package com.qiankun.tang.ui.view.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.common.library.listener.OnAppListener;
import com.common.library.tools.Logger;
import com.common.library.ui.view.adapter.base.BasePagerAdapter;
import com.facebook.drawee.view.SimpleDraweeView;
import com.qiankun.tang.beans.ADBean;
import com.qiankun.tang.ui.view.ADVidoVeiw;

/**
 * @author txf
 * @Title
 * @package com.common.library.ui.view.adapter
 * @date 2017/6/6 0006
 */
public class ADPagerAdapter extends BasePagerAdapter<ADBean> {
    public ADPagerAdapter(OnAppListener.OnInteractionListener l) {
        super(l);
    }
    private int getItemType(int position) {
        if (getData() != null && getData().size() > 0)
            return getData().get(position).getType();
        else {
            return -1;
        }
    }
    @Override
    protected void removeItemView(int position, View view) {
        if (view instanceof ADVidoVeiw) {
            Logger.i("removeItemView  停止播放");
            ADVidoVeiw mADVidoVeiw = (ADVidoVeiw) view;
            mADVidoVeiw.stopPlayer();
        }
    }
    @Override
    protected View getView(int position, View contentView, ViewGroup container) {
        int type = getItemType(position);
        switch (type) {
            case 0:
                SimpleDraweeView simpleDraweeView;
                if (contentView == null) {
                    simpleDraweeView = new SimpleDraweeView(container.getContext());
                    simpleDraweeView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                    simpleDraweeView.setScaleType(ImageView.ScaleType.FIT_XY);
                } else {
                    simpleDraweeView = (SimpleDraweeView) contentView;
                }
                final ADBean bean = getData().get(position);
                simpleDraweeView.setImageURI(bean.getImgUrl());
                simpleDraweeView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getListener().onInteraction(0, bean);
                    }
                });
                return simpleDraweeView;
            case 1:
                ADVidoVeiw adVidoVeiw;
                if (contentView == null) {
                    adVidoVeiw = new ADVidoVeiw(container.getContext());
                    adVidoVeiw.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                } else {
                    adVidoVeiw = (ADVidoVeiw) contentView;
                }
                final ADBean bean1 = getData().get(position);
                adVidoVeiw.setData(bean1);
                adVidoVeiw.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getListener().onInteraction(0, bean1);
                    }
                });
                return adVidoVeiw;
        }
        return null;
    }
}
